home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / setenv13.zip / SETENV.C < prev    next >
Text File  |  1989-09-28  |  4KB  |  154 lines

  1. #include <stdio.h>
  2. #include <dos.h>
  3. #include <dir.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. int settheenv (char * symbol, char * val);
  8.  
  9. #define MYINT  0xD3
  10.  
  11. union  REGS  regs;
  12. struct SREGS sregs;
  13. int    largc;
  14. char   **largv;
  15.  
  16. void display_help() {
  17. puts("\nSETENV 1.3  by Richard Marks");
  18. puts("     Sets environment variables to a user response (for BAT files)");
  19. puts("     SETENV  <envirn vbl name>  <prompt message>\n");
  20. puts("Example:");
  21. puts("     setenv drv \"PLEASE ENTER DRIVE TO USE : \" ");
  22. puts("the message PLEASE ENTER DRIVE TO USE : is displayed on the console,");
  23. puts("the response is set into variable drv for use by the rest of the BAT file\n");
  24. puts("If there is a keyword instead of a <message>, SETENV will fill certain system");
  25. puts("values into the environment variable:\n");
  26. puts("     SETENV  <envirn vbl name>  %cwd  -  get current working directory");
  27. puts("                                %drive-  get current drive");
  28. puts("                                %dosv -  get dos major version");
  29. puts("                                %num  -  a unique number");
  30. puts("                                %fs file - get size of file");
  31. puts("                                %fn file - just file name (no drive or dir)");
  32. puts("Use %% to represent a single % in BAT files\n");
  33. puts("Multiple sets of arguments can be supplied:");
  34. puts("             SETENV <vbl> <msg> <vbl> <msg> . . .");
  35. }
  36.  
  37. char get_default_drive () {
  38.     regs.h.ah = 0x19;
  39.     intdos(®s, ®s);
  40.     return (regs.h.al);
  41. }
  42.  
  43.  
  44. long filesize (char *filename) {
  45.     struct ffblk fff;
  46.     if (findfirst (filename, &fff, 0xff) != 0)  return (0);
  47.     return (fff.ff_fsize);
  48. }
  49.  
  50.  
  51. char get_special_param(char *val, int *argbase) {
  52. /* plugs the system parmeter specified in the arg into val
  53.    %cwd  = current directory path
  54.    %drive= default drive
  55.    %dosv = dos major version */
  56.     char *func;
  57.     int  i;
  58.     long nn;
  59.  
  60.     if ( ++(*argbase)>=largc ) return (2);
  61.     func = largv[*argbase];
  62.  
  63.     if (stricmp(func,"%CWD")==0) {
  64.         getcwd(val, 128);
  65.  
  66.     } else if (stricmp(func,"%DRIVE")==0) {
  67.         val[0] = get_default_drive() + 'A';
  68.         val[1] = 0;
  69.  
  70.     } else if (stricmp(func,"%DOSV")==0) {
  71.         val[0] = _osmajor+'0';
  72.         val[1] = 0;
  73.  
  74.     } else if (stricmp(func,"%NUM")==0) {
  75.         nn = (long) getvect(MYINT);
  76.         setvect (MYINT, (void interrupt (*)()) ++nn);
  77.         ltoa (nn, val, 10);
  78.         val [3] = 0;
  79.  
  80.     } else if (stricmp(func,"%FS")==0) {
  81.         if ( ++(*argbase)>=largc ) return (2);
  82.         ltoa (filesize(largv[*argbase]), val, 10);
  83.  
  84.     } else if (stricmp(func,"%FN")==0) {
  85.         if ( ++(*argbase)>=largc ) return (2);
  86.         i = strlen(func =largv[*argbase]);
  87.         while (i!=0) {
  88.             if (func[--i]=='\\'  || func[i]==':') {
  89.                 i++;  break;
  90.             }
  91.         }
  92.         strcpy (val, &func[i]);
  93.  
  94.     } else
  95.         return(2);
  96.  
  97.     return(0);
  98. }
  99.  
  100.  
  101. char get_user_input(char *val, int *argbase) {
  102. /* solicits user for a value (val) by displaying the message (msg).
  103.    then validates the message using criteria in arg.  Criteria are:
  104.    <null>  no criteria test for now */
  105.  
  106.     if ( ++(*argbase) >= largc ) return (2);
  107.     do {
  108.        printf("%s", largv[*argbase]);
  109.        gets(val);
  110.        } while (0);
  111.     return(0);
  112.     }
  113.  
  114.  
  115. char main(int argc, char *argv[]) {
  116. int argbase, ret;
  117. char symbol[16], val[128];
  118.  
  119. largc = argc;
  120. largv = argv;
  121.  
  122. if (largc<3) {
  123. HELP_RET:
  124.     display_help(); return(1);
  125. }
  126.  
  127. for (argbase=1; argbase<argc; argbase++) {
  128.  
  129.     if ( strlen(largv[argbase])>16 ) goto HELP_RET;
  130.     strcpy(symbol, largv[argbase]);  strupr(symbol);
  131.  
  132.     if (largv[argbase+1][0]=='%') {
  133.         ret = get_special_param(val, &argbase);
  134.     } else {
  135.         ret = get_user_input(val, &argbase);
  136.     }
  137.  
  138.     if (ret == 0)
  139.         ret = settheenv(symbol, val);
  140.  
  141.     if (ret != 0) {
  142.         if (ret == 2)  goto HELP_RET;
  143.         goto ERROR_RET;
  144.     }
  145. }
  146. return(0);
  147.  
  148. ERROR_RET:
  149. #ifdef DEBUG
  150. puts("SETENV failed");
  151. #endif
  152. return(1);
  153. }
  154.